package es.cazadoresmadridejos.beans.controller.event;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.event.EventManager;
import es.cazadoresmadridejos.beans.validator.event.EventValidator;
import es.cazadoresmadridejos.model.Event;
import es.cazadoresmadridejos.model.Event.EventType;
import es.cazadoresmadridejos.model.Partner.Sex;
import es.cazadoresmadridejos.propertyeditor.EventPropertyEditor;
import es.cazadoresmadridejos.util.model.ReasonUtil;
import es.cazadoresmadridejos.util.validation.ValidationContainer;
import es.cazadoresmadridejos.web.util.ListUtil;


@Controller
public class EventCreateModifyController {

	@Autowired private EventManager eventManager;
	@Autowired private EventValidator eventValidator;
	@Autowired private MessageSource messageSource;

	@InitBinder
	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Throwable{
		binder.registerCustomEditor(Event.class, new EventPropertyEditor(eventManager));
	}

	@RequestMapping(value="/eventCreateModify.html",method=RequestMethod.GET)
	public ModelAndView createModify(
			@RequestParam(value="id",required=false) Event event) throws Throwable {
		ModelAndView mv = new ModelAndView();
		if(event == null){
			event = new Event();
		}
		mv.addObject("event",event);

		Calendar cal = Calendar.getInstance();
		if(cal.get(Calendar.MONTH) < 3){
			mv.addObject("startYear",cal.get(Calendar.YEAR) - 1);
			mv.addObject("endYear",cal.get(Calendar.YEAR));
		}else{
			mv.addObject("startYear",cal.get(Calendar.YEAR));
			mv.addObject("endYear",cal.get(Calendar.YEAR) + 1);
		}
		mv.addObject("event",ReasonUtil.getCurrentReason());
		mv.addObject("eventTypes",ListUtil.toString(EventType.values()));

		return mv;
	}
	@RequestMapping(value="/eventCreateModify.html",method=RequestMethod.POST)
	public ModelAndView submit(
			@ModelAttribute("event") Event event, 
			BindingResult result, 
			SessionStatus status,
			HttpServletRequest request
			) throws Throwable {

		ModelAndView mv = new ModelAndView("redirect:index.html");
		mv.addObject("screen"," eventList.html");

		List<ValidationContainer> errors =  eventValidator.validate( event);
		if(errors.isEmpty()){
			mv.addObject("okMessage", "THE_event_WAS_CREATED_SUCCESSFULLY");
		}else{
			mv.addObject("errors",errors);
		}

		eventManager.save(event);

		return mv;
	}
}
